package win.zxshuo.www.controller.apis;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import win.zxshuo.www.controller.responses.BaseResponse;
import win.zxshuo.www.controller.validators.UserValidator;
import win.zxshuo.www.model.UserModel;
import win.zxshuo.www.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/user")
public class UserApiController {

    Log logger = LogFactory.getLog(getClass());

    @Autowired
    UserService userService;

    // 用户相关操作类
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public BaseResponse userLoginApi(@Validated({UserValidator.Username.class, UserValidator.Password.class}) UserValidator userValidator,
                                     BindingResult result,
                                     HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        if (result.hasErrors()){
            baseResponse.setErrBindingResult(result);
            return baseResponse;
        }
        try {
            UserModel userModel = userService.checkUser(userValidator.getUsername(),userValidator.getPassword());
            if (userModel != null){
                session.setAttribute("token",userModel.getToken());
                session.setAttribute("mobile",userModel.getMobile());
                session.setAttribute("username",userModel.getUsername());
                Map<String,String> data = new HashMap<>();
                data.put("token",userModel.getToken());
                baseResponse.setData(data);
                baseResponse.setTrue();

            }else {
                baseResponse.setErrorMsg("用户名或者密码错误");
            }
        }catch (Exception e){
            logger.error(e.getCause());
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }


    @RequestMapping(value = "/wxlogin",method = RequestMethod.POST)
    public BaseResponse wxUserLogin(){
        BaseResponse baseResponse = new BaseResponse();



        return baseResponse;
    }


    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public BaseResponse userRegister(@Validated({UserValidator.Username.class, UserValidator.Password.class, UserValidator.Mobile.class}) UserValidator userValidator,
                                     BindingResult result,
                                     HttpSession session){
        BaseResponse baseResponse =new BaseResponse();
        if (result.hasErrors()){
            baseResponse.setErrBindingResult(result);
            return baseResponse;
        }
        try {
            UserModel userModel = userService.addNewUser(userValidator.getUsername(),userValidator.getPassword(),userValidator.getMobile());
            session.setAttribute("token",userModel.getToken());
            session.setAttribute("mobile",userModel.getMobile());
            session.setAttribute("username",userModel.getUsername());
            Map<String,String> data = new HashMap<>();
            data.put("token",userModel.getToken());
            baseResponse.setData(data);
            baseResponse.setTrue();
        }catch (Exception e){
            logger.error(e.getCause());
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }
}

